package com.lanhao.aot.carrierintegration.emiratespost;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;

public class EmiratesPostUtil {
	private static final Logger LOG = LoggerFactory.getLogger(EmiratesPostUtil.class);
	public static final String BASE_URL = "https://osbtest.epg.gov.ae/ebs/genericapi/booking/rest";
	public static final String BOOKING_URL = BASE_URL + "/CreateBooking";
	public static final String PRINGT_URL = BASE_URL + "/PrintLabel";
	public static final String ACCOUNT_NO = "C175120";
	public static final String PASSWORD = "C175120";
	public static final String COUNTRY_CODE = "971";
	public static final SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);

	public static void main(String[] args) {
		Map<String, String> headerMap = new HashMap<String, String>();
		headerMap.put("Content-Type", "application/json");
		headerMap.put("AccountNo", ACCOUNT_NO);
		headerMap.put("Password", PASSWORD);
		JSONObject requestBody = new JSONObject();
		JSONObject BookingRequest = new JSONObject();
		// 发件人信息
		BookingRequest.put("SenderContactName", "KunKun");
//		BookingRequest.put("SenderCompanyName", "");
		BookingRequest.put("SenderAddress", "AnHe Test Address");
		BookingRequest.put("SenderCity", 1);  // 怎么做转换
		BookingRequest.put("SenderContactMobile", "512354123");
		BookingRequest.put("SenderContactPhone", "1234123");
//		BookingRequest.put("SenderEmail", "sendersemail@gmail.com");
//		BookingRequest.put("SenderZipCode", "00000");
		BookingRequest.put("SenderState", "Dubai");
		BookingRequest.put("SenderCountry", COUNTRY_CODE);
		// 收件人信息
		BookingRequest.put("ReceiverContactName", "KunZhou");
//		BookingRequest.put("ReceiverCompanyName", "");
		BookingRequest.put("ReceiverAddress", "AnHe Test ReceiverAddress");
		BookingRequest.put("ReceiverCity", 1);
		BookingRequest.put("ReceiverContactMobile", "14222212");
		BookingRequest.put("ReceiverContactPhone", "443124");
		BookingRequest.put("ReceiverEmail", "it@anhelogistics.com");
//		BookingRequest.put("ReceiverZipCode", "00000");
		BookingRequest.put("ReceiverState", "Dubai");
		BookingRequest.put("ReceiverCountry", COUNTRY_CODE);
		// 其他信息
//		BookingRequest.put("ReferenceNo", "Ref - 012");
//		BookingRequest.put("ReferenceNo1", "");
//		BookingRequest.put("ReferenceNo2", "");
//		BookingRequest.put("ReferenceNo3", "");
		BookingRequest.put("ContentTypeCode", "NonDocument"); // Document, NonDocument
		BookingRequest.put("NatureType", 11);
		BookingRequest.put("Service", "Domestic"); // Domestic, International, InternationalIncoming
		BookingRequest.put("ShipmentType", "Express"); // Express, Premium, PrimeRegistered, PrimeExpress, PrimeTracked
		BookingRequest.put("DeleiveryType", "Door to Door"); // Door to Door, Counter
		BookingRequest.put("Registered", "No"); // Yes, No
		BookingRequest.put("PaymentType", "Credit"); // Credit, COD, Cash
//		BookingRequest.put("CODAmount", "0");
//		BookingRequest.put("CODCurrency", "");
//		BookingRequest.put("CommodityDescription", "Any good description");
		BookingRequest.put("Pieces", 1);
		BookingRequest.put("Weight", 500);
		BookingRequest.put("WeightUnit", "Grams"); // Grams, Kilograms
		BookingRequest.put("Length", "12");
		BookingRequest.put("Width", "12");
		BookingRequest.put("Height", "12");
		BookingRequest.put("DimensionUnit", "Centimetre"); // Centimetre, Meter
//		BookingRequest.put("ItemValue", "0");
//		BookingRequest.put("ValueCurrency", "AED");
//		BookingRequest.put("ProductCode", "");  // 
//		BookingRequest.put("DeliveryInstructionsID", "");
//		BookingRequest.put("RequestSource", "");
		BookingRequest.put("SendMailToSender", "No");
		BookingRequest.put("SendMailToReceiver", "No");
		String pickupDate = sdf.format(DateUtil.offsetDay(new Date(), 1));
		BookingRequest.put("PreferredPickupDate", pickupDate);
		BookingRequest.put("PreferredPickupTimeFrom", "15:00");
		BookingRequest.put("PreferredPickupTimeTo", "18:00");
		BookingRequest.put("Is_Return_Service", "Yes"); // Yes, No
		BookingRequest.put("PrintType", "AWBOnly"); // None, LabelOnly, AWBOnly, AWBandLabel
//		BookingRequest.put("Latitude", "40.689263");
//		BookingRequest.put("Longitude", "74.044505");
//		BookingRequest.put("TransactionSource", "");
		BookingRequest.put("AWBType", "EAWB"); // EAWB, None
		BookingRequest.put("RequestType", "Booking"); // Shipment, Booking
//		BookingRequest.put("Remarks", "");
//		BookingRequest.put("SpecialNotes", "Something to be printed");
//		BookingRequest.put("SenderRegionId", "");
//		BookingRequest.put("ReceiverRegionId", "");
//		BookingRequest.put("AWBNumber", "");
		
		requestBody.put("BookingRequest", BookingRequest);

		LOG.info("开始请求阿联酋邮政requestBody={}", requestBody);
		String resp = HttpRequest.post(BOOKING_URL).addHeaders(headerMap).timeout(10000).body(requestBody.toString()).execute().body();
		LOG.info("结束请求阿联酋邮政返回结果resp={}", resp);
		try {
			FileUtils.write(new File("D:/EmiratesPost.txt"), resp, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONObject respObject = JSON.parseObject(resp);
		String errorMessage = respObject.getString("errorMessage");
		if (StringUtils.isNotBlank(errorMessage)) {
			LOG.info("请求阿联酋邮政返回失败，errorMessage={}", errorMessage);
			return;
		}
		JSONObject bookingObject = respObject.getJSONObject("BookingResponse");
		JSONObject serviceResultObject = bookingObject.getJSONObject("serviceResult");
		boolean success = serviceResultObject.getBoolean("success");
		if (success) {
			String AWBNumber = bookingObject.getString("AWBNumber");
			String AWBLabel = bookingObject.getString("AWBLabel");
			if (StringUtils.isNotBlank(AWBLabel)) {
				Base64.decodeToFile(AWBLabel, new File("D:/" + AWBNumber + ".pdf"));
			}
			LOG.info("请求阿联酋邮政返回成功，AWBNumber={}", AWBNumber);
		} else {
			String errorMsg = serviceResultObject.getString("ErrorMsg");
			LOG.info("请求阿联酋邮政返回失败，errorMessage={}", errorMsg);
		}

	}
}
